package com.hkd.forum.service.impl;

import com.hkd.forum.common.AppResult;
import com.hkd.forum.common.ResultCode;
import com.hkd.forum.dao.BoardMapper;
import com.hkd.forum.exception.ApplicationException;
import com.hkd.forum.model.Board;
import com.hkd.forum.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Autowired
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {

        //非空校验
        if (num <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //调用Dao查询数据库数据
        List<Board> result = boardMapper.selectByNum(num);
        //返回
        return result;
    }

    @Override
    public Board selectById(Long id) {
        //非空检验
        if(id == null || id <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //调用dao查询数据
        Board board = boardMapper.selectByPrimaryKey(id);
        //返回
        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //非空检验
        if(id == null || id <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            //打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString() + ",board id = "+ id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }

        //更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        boardMapper.updateByPrimaryKeySelective(updateBoard);

    }

    @Override
    public void subOneArticleCountById(Long id) {
        //非空检验
        if(id == null || id <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + ",board id = "+ id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        //判断减一后是否小于零
        if(updateBoard.getArticleCount() < 0){
            //设置为 0
            updateBoard.setArticleCount(0);
        }
        //调用dao
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //判断更新结果
        if(row != 1){
            //打印日志
            log.warn(ResultCode.FAILED.toString() + ",board id = "+ id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
